package com.buptlogistics.creditinvestigation.fragment;


import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buptlogistics.creditinvestigation.R;
import com.buptlogistics.creditinvestigation.activity.LoginRegistActivity;
import com.buptlogistics.creditinvestigation.app.AppData;
import com.buptlogistics.creditinvestigation.app.BaseFragment;


/**
 * A simple {@link Fragment} subclass.
 */
public class MoreFragment extends BaseFragment {

    private View view;

    private RelativeLayout rl_dismiss;
    private TextView tv_dismiss;

    public MoreFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_more, container, false);
        AppData.getInstance(MoreFragment.this.getActivity());
        initViews();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initEvents();
    }

    protected void initViews() {
        rl_dismiss = (RelativeLayout) findViewById(R.id.rl_more_dimiss);
        tv_dismiss = (TextView) findViewById(R.id.tv_more_dimiss);
    }

    protected void initEvents() {
        if (AppData.getInstance(MoreFragment.this.getActivity()).getIsLogin()) {
            tv_dismiss.setText("注  销");
            rl_dismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(MoreFragment.this.getActivity());
                    builder.setMessage("确定注销");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            AppData.getInstance(MoreFragment.this.getActivity()).setLogin(false);
                            AppData.getInstance(MoreFragment.this.getActivity()).saveTooken("");
                            AppData.getInstance(MoreFragment.this.getActivity()).saveUserInfo("");
                            MoreFragment.this.onResume();
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    });
                    builder.show();
                }
            });
        } else {
            tv_dismiss.setText("登  录");
            rl_dismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(LoginRegistActivity.class);
                }
            });
        }
    }

    protected View findViewById(int id) {
        return view.findViewById(id);
    }

}
